package com.jbm.jbmsupplier.zp.FactouryActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.onehome.common.OHInterface;
import com.jbm.onehome.iface.LoginEntity;

import java.util.ArrayList;
import java.util.HashMap;

import static com.jbm.jbmsupplier.zp.service.MyService.isOn;
import static com.jbm.jbmsupplier.zp.utils.TableName.BUDDY;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHAT;
import static com.jbm.jbmsupplier.zp.utils.TableName.RECENT;

/**
 * Created by 家帮忙 on 2017/7/11.
 */

public class MyNewpay implements ChatSaveMessage {
    private ArrayList<String> odername;
    private ArrayList<HashMap<String, String>> list1;
    private  Context context;
    private ArrayList<ChatMsgEntity> arrayList;
    private ClassUser ClassUser;
    private final int USERMASSAGES=0x004;
    private final int TIME=0x0001;
    private Buddy_ListData namse;

    @Override
    public void setDataChat(Context context, ArrayList<ChatMsgEntity> arrayList, ClassUser ClassUser) {
        this.context=context;
        this.arrayList=arrayList;
        this.ClassUser=ClassUser;
//        list1 = MySqliteUtlis.GetInstan(context).showalldata(BUDDY ,arrayList.get(0).getTo_id());
//        if (list1.toString() == null) {
//            isOn = false;
//            hadnler.sendEmptyMessageDelayed(TIME, 2000);
//        } else if (!list1.toString().contains(arrayList.get(0).getTo_id()+"")){
//            isOn = false;
//            hadnler.sendEmptyMessageDelayed(TIME, 2000);
//        }else {
        namse = MySqliteUtlis.GetInstan(context).showFriendsToID(BUDDY, arrayList.get(0).getTo_id(), ClassUser.getLogin());
        if (namse==null) {
            MySqliteUtlis.GetInstan(context).StoringChatData(CHAT, arrayList.get(0).getTo_id(), OHInterface.getG_server().logo, arrayList.get(0).getText(), arrayList.get(0).getDate(),"", "", "", "", "", "", "", "", "", "", 8, 0, "true", ClassUser.getLogin());
        }else{
            MySqliteUtlis.GetInstan(context).StoringChatData(CHAT, arrayList.get(0).getTo_id(), OHInterface.getG_server().logo, arrayList.get(0).getText(), arrayList.get(0).getDate(),namse.getNickname(), "", "", "", "", "", "", "", "", "", 8, 0, "true", ClassUser.getLogin());
        }
            ArrayList<String> als = MySqliteUtlis.GetInstan(context).showaRecentContactsid(RECENT,arrayList.get(0).getTo_id(),ClassUser.getLogin());
            if (!als.toString().contains(arrayList.get(0).getTo_id()+"")) {
                MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT, arrayList.get(0).getTo_id(),ClassUser.getLogin(), arrayList.get(0).getDate());
            }else if (als.toString()==null){
                MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT ,arrayList.get(0).getTo_id(),ClassUser.getLogin(), arrayList.get(0).getDate());
            }
                setBroadCase(14,arrayList);
                arrayList.remove(0);

//        }
    }



    /**
     * 获取好友信息
     * */
public void setUserMassage(int toid){
//    NetinterSend netintersend=new NetinterSend(hadnler);
//              netintersend.getUerMessage(toid,USERMASSAGES);
}

    Handler hadnler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
           switch (msg.what){
               case USERMASSAGES:
                   LoginEntity loginmassage = (LoginEntity) msg.obj;
                   if (loginmassage.m_code == 200) {
                       MySqliteUtlis.GetInstan(context).StoringBuddydata(BUDDY ,loginmassage.getLoginId(), loginmassage.getNickname(), "", loginmassage.getAccSecretEntity().getMobile(), "", loginmassage.getLogo(), "", "", "", 0,ClassUser.getLogin());
                       MySqliteUtlis.GetInstan(context).StoringChatData(CHAT , loginmassage.getLoginId(), OHInterface.getG_server().logo, "", arrayList.get(0).getDate(), MySqliteUtlis.GetInstan(context).showFriendsToID(BUDDY , arrayList.get(0).getTo_id(),ClassUser.getLogin()).getNickname(), "", "", "", arrayList.get(0).getPayDate(), arrayList.get(0).getPayMoney(), arrayList.get(0).getPayMode(), "收款成功", "", "", 14, 0,"true", ClassUser.getLogin());
                       ArrayList<String> als = MySqliteUtlis.GetInstan(context).showaRecentContactsid(RECENT,loginmassage.getLoginId(),ClassUser.getLogin());
                       if (als.toString()==null) {
                           MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT ,loginmassage.getLoginId(),ClassUser.getLogin(), arrayList.get(0).getDate());
                       }else if(!als.toString().contains(loginmassage.getLoginId()+"")){
                           MySqliteUtlis.GetInstan(context).StoringRecentID(RECENT ,loginmassage.getLoginId(),ClassUser.getLogin(), arrayList.get(0).getDate());

                       }
//                           startForeground(12346, model.getNotification(MySqliteUtlis.GetInstan(getBaseContext()).showFriendsToID(BUDDY + ClassUser.getLogin(), arrayList.get(0).getTo_id()).getNickname(), "收款", 1, getBaseContext(), arrayList.get(0).getTo_id()));
                       setBroadCase(14, arrayList);
                           arrayList.remove(0);
                           isOn = true;
                   }
                   break;
               case TIME:
                   setUserMassage(arrayList.get(0).getTo_id());
                   break;
           }
            super.handleMessage(msg);

        }
    };

    private void setBroadCase(int id,ArrayList<ChatMsgEntity> arrayList){
        Intent intent = new Intent();
        intent.putExtra("messageFlag", id);
        intent.putExtra("account_id", arrayList.get(0).getTo_id());
        intent.putExtra("Date", arrayList.get(0).getDate());
        intent.putExtra("paydate", arrayList.get(0).getPayDate());
        intent.putExtra("fee", arrayList.get(0).getPayMoney());
        intent.putExtra("paytype", arrayList.get(0).getPayMode());
        intent.setAction("MSG");
        context.sendBroadcast(intent);
    }



}
